package com.jjy.admin.utils;

import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;

public class WebDriverUtil {
    private static String webDriverDir ;
    private static WebDriver edgeDriver = null;

    static {
        webDriverDir = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\MicrosoftWebDriver.exe";
        // 加载驱动
        System.setProperty("webdriver.edge.driver", webDriverDir);
    }


        /**
         * 获取web驱动
         *
         * @return 浏览器驱动
         */
    public static WebDriver getWebDriver() {


        // 设置浏览器参数
        EdgeOptions options = new EdgeOptions();
        HashMap<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("images", 2);
        prefs.put("javascript", 2);
        prefs.put("permissions.default.stylesheet", 2);
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);
        prefs.put("profile.default_content_setting_values", false);
        options.setExperimentalOption("prefs", prefs);
        options.setImplicitWaitTimeout(Duration.ofSeconds(20));
//        使用无痕模式，规避弹窗
        options.addArguments("-inprivate");
//        无头模式
        options.addArguments("--headless");
        //窗口最大化

        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--remote-allow-origins=*");
        options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
//        options.addArguments("--disable-blink-features");
        options.setExperimentalOption("useAutomationExtension", false);
        options.setPageLoadStrategy(  PageLoadStrategy.NORMAL);
        // 实例化驱动对象
        edgeDriver = new EdgeDriver(options);

        // 去除seleium全部指纹特征
//        FileReader fileReader = new FileReader("E:\\ideaWorks\\SpringWind-spring-mvc\\SpringWind\\src\\test\\java\\test\\huakuai\\stealth.min.js");
//        String js = fileReader.readString();
//        edgeDriver.executeScript(js);
        return edgeDriver ;
    }

//    关闭资源
    public static void closeWebDriver() {
        edgeDriver.close();
        edgeDriver.quit();
    }

    }
